package cn.edu.swufe.stu2025.myapplication;

import android.content.Intent;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class RMB extends AppCompatActivity {
    TextView result;
    EditText rmb;
    Button btn1,btn2,btn3;
    float dollar=6.33f;
    float euro=33.22f;
    float won=345.5f;
    private static final String TAG = "RMB";
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_rmb);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    public void Click(View v){
        rmb=findViewById(R.id.input);
        result=findViewById(R.id.result);

        Log.i(TAG, "Click: ");
        try{
            float ss=Float.parseFloat(String.valueOf(rmb.getText()));
            if(v.getId()==R.id.dollor){
                ss=ss*dollar;
            }
            else if(v.getId()==R.id.euro){
                ss=ss*euro;
            }
            else{
                ss=ss*won;
            }
            result.setText("结果是："+Float.toString(ss));
        }
        catch(NumberFormatException e){
            //提示出错
        }
    }
    ActivityResultLauncher launcher =registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result->{
                if(result.getResultCode()==3){
                    Intent retIntent = result.getData();
                    Bundle retBundle = retIntent.getExtras();
                    dollar = retBundle.getFloat("ret_dollar");
                    euro = retBundle.getFloat("ret_euro");
                    won =retBundle.getFloat("ret_won");
                    Log.i(TAG, "launcher is working");
                }
            });
    public void openConfig(View btn){
        //打开新窗口
        Intent intent = new Intent(this,settingActivity.class);
        intent.putExtra("key_dollar",dollar);
        intent.putExtra("key_euro",euro);
        intent.putExtra("key_won",won);
        intent.putExtra("myarg","hello_swufe");

        startActivityForResult(intent,6);
    }

    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        //从data中读取数据

    }
}